package com.rcm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.code.kaptcha.Constants;
import com.rcm.model.Studentinfo;
import com.rcm.utility.JavaMailUtil;
import com.rcm.utility.RandomUtil;
import com.rcm.utility.htmlText;


//注册- 发送邮箱验证码功能
@WebServlet("/SendEmailServlet")
public class SendEmailServlet extends HttpServlet {
	int count=0;
	private static final long serialVersionUID = 1L;
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
			throws ServletException, IOException {
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter out =resp.getWriter();
		HttpSession session=req.getSession();
		String vcode=req.getParameter("vcode");
		String name=req.getParameter("name");
		String make_vcode=(String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
		System.out.println(make_vcode);
		if(make_vcode.equalsIgnoreCase(vcode)) {
			System.out.println("邮箱发送功能");
			try {
				String email ="";
				Studentinfo c = (Studentinfo) req.getSession().getAttribute("c");
				//修改密码的时候判断 防止前端修改文本框
				if(c==null) {
					 email = req.getParameter("email");
				}else {
					 email = c.getEmail();
					 name=c.getName();
				}
				System.out.println("向发送验证码："+email);
				JavaMailUtil.receiveMailAccount = email; // 给用户输入的邮箱发送邮件

				// 1、创建参数配置，用于连接邮箱服务器的参数配置
				Properties props = new Properties();
				// 开启debug调试
				props.setProperty("mail.debug", "true");
				// 发送服务器需要身份验证
				props.setProperty("mail.smtp.auth", "true");
				// 设置右键服务器的主机名
				props.setProperty("mail.host", JavaMailUtil.emailSMTPHost);
				// 发送邮件协议名称
				props.setProperty("mail.transport.protocol", "smtp");
				// 2、根据配置创建会话对象，用于和邮件服务器交互
				Session session1 = Session.getInstance(props);
				// 设置debug，可以查看详细的发送log
				session1.setDebug(true);
				// 3、创建一封邮件
				String code = RandomUtil.getRandom();
				System.out.println("邮箱验证码：" + code);
				String html = htmlText.html(code,name);
				MimeMessage message = JavaMailUtil.creatMimeMessage(session1, JavaMailUtil.emailAccount,
						JavaMailUtil.receiveMailAccount, html);
				// 4、根据session获取邮件传输对象
				Transport transport = session1.getTransport();
				// 5、使用邮箱账号和密码连接邮箱服务器emailAccount必须与message中的发件人邮箱一致，否则报错
				transport.connect(JavaMailUtil.emailAccount, JavaMailUtil.emailPassword);
				// 6、发送邮件,发送所有收件人地址
				transport.sendMessage(message, message.getAllRecipients());
				// 7、关闭连接
				transport.close();
				//  写入session
				req.getSession().setAttribute("code", code);
			} catch (Exception e) {
				e.printStackTrace();
				req.getSession().setAttribute("error", "邮件发送失败");
			}
		}else {
			System.out.println("验证码错误");
			out.print(0);
			out.flush();
			out.close();

			//req.getRequestDispatcher("/register.jsp").forward(req, resp);
		}
	}
}
